package com.example.wangda.smart_home_android_v12;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import com.chinamobile.iot.onenet.mqtt.MqttClient;
import com.chinamobile.iot.onenet.mqtt.protocol.MqttSubscribe;

import static com.chinamobile.iot.onenet.mqtt.protocol.imp.QoS.AT_MOST_ONCE;

/**
 * Created by wangda on 2017/10/24.
 */


public class MqttThread extends Thread{

        private static final String TAG = "_MqttThread_DEBUG_";
        MqttThread(Context context){

    }

    /*作者：王达
    * MQTT耗时任务处理线程
    * */
    public void StartThread()
    {
        LooperThread loopThread = new LooperThread();
        loopThread.start();
        //new Thread(new thread()).start();
    }

    class thread implements Runnable{
        @Override
        public void run(){

            while(true){
                try{
                    Thread.sleep(5000);

                }catch(Exception e){
                    Log.d(TAG, "catch = " + e);
                }
            }
        }
    }

    class LooperThread extends Thread{
        private static final String TAG = "_LooperThread_DEBUG_";
        public Handler mHandler;
        public void run(){
            Looper.prepare();
            mHandler = new Handler(){
                public void handleMessage(Message msg){
                    try{
                        for (String topic:MqttPro.TOPIC) {
                            MqttClient.getInstance().subscribe(new MqttSubscribe(topic, AT_MOST_ONCE));
                            Log.d(TAG, topic + ": subscribe");
                            Thread.sleep(5000);
                        }
                    }catch(RuntimeException e){
                        Log.d(TAG, e.toString());
                    }catch (Exception e){
                        Log.d(TAG, "catch = " + e);
                    }
                }
            };
            mHandler.sendEmptyMessage(1);
            Looper.loop();
        }
    }
}


